/* Copyright (C) 2014-2015 Mammoth Inc. */

var m = new M("passport");

$(function() {
	$("input,textarea").placeholder();
	$("input:first").focus();
	$("input").on("keypress", function(e) {
		var code = e.keyCode || e.which;
		if (code == 13) {
			userLogin();
		}
	});
	$("#login-success-modal").on("hidden.bs.modal", function() {
		window.location.href = $(this).data("url");
	});
	$("#weixin_icon").mouseover(function() {
		$(this).tooltip('show');
	});
	$("#weibo_icon").mouseover(function() {
		$(this).tooltip('show');
	});
	$("#github_icon").mouseover(function() {
		$(this).tooltip('show');
	});
	$("#renren_icon").mouseover(function() {
		$(this).tooltip('show');
	});
	$("#mm_email_log").blur(function() {
		$("#mm_mail").html("");
		$(this).removeClass("mm_form_error");
	});
	$("#mm_password_log").blur(function() {
		$(this).removeClass("mm_form_error");
	});
	var userLogin = function() {
		var $mm_email_log = $("#mm_email_log");
		var $mm_password_log = $("#mm_password_log");
		if (($mm_email_log.val() == "") || ($mm_password_log.val() == "")) {
			$("#mm_mail").html("您输入的帐号或者密码错误");
			$mm_email_log.addClass("mm_form_error");
			$mm_password_log.addClass("mm_form_error");
		} else {
			if (/^((\+?86)|(\(\+86\)))?1\d{10}$/.test($mm_email_log.val())) {
				$.ajax({
					type: "POST",
					url: "/api/auth/loginbyphone/",
					data: {
						phone_num: $mm_email_log.val(),
						pwd: M.crypto.md5($mm_password_log.val())
					},
					dataType: "json",
					success: function(data) {
						if (data.code == "0") {
							if($("#mm_phoneLogFlag").val()=="1"){
								window.location.href = "http://exam.mengma.com/index/indexmobile";
							}else{
								getReferrer();
							}
						} else if (data.code == "400003") { /*手机号或者密码错误未注册*/
							$("#mm_mail").html("您输入的帐号或者密码错误");
							$mm_email_log.addClass("mm_form_error");
							$mm_password_log.addClass("mm_form_error");
						} else if (data.code == "400008") { /*手机号未注册*/
							$("#mm_mail").html('您输入的帐号或者密码错误');
							$mm_email_log.addClass("mm_form_error");
							$mm_password_log.addClass("mm_form_error");
						} else {
							window.location.href = "http://www.mengma.com/";
						}
					}
				});
			} else {
				$.ajax({
					type: "POST",
					url: "/api/auth/login/",
					data: {
						email: $mm_email_log.val(),
						pwd: M.crypto.md5($mm_password_log.val())
					},
					dataType: "json",
					success: function(data) {
						if (data['code'] == "0") {

							if($("#mm_phoneLogFlag").val()=="1"){
								window.location.href = "http://exam.mengma.com/index/indexmobile";
							}else{
								getReferrer();
							}

						} else if (data['code'] == "400003") {
							$("#mm_mail").html("您输入的帐号或者密码错误");
							$mm_email_log.addClass("mm_form_error");
							$mm_password_log.addClass("mm_form_error");
						} else if(data['code']=="400015"){
							$("#mm_mail").html('您输入的帐号或者密码错误');
							$mm_email_log.addClass("mm_form_error");
							$mm_password_log.addClass("mm_form_error");
						}else {
							window.location.href = "http://oj.mengma.com/";
						}
					}
				});
			}
		}
	}
	$("#mm_btn_login").click(userLogin);

	function getReferrer(){
		var referrer = "";
		try {
	        referrer = window.top.document.referrer;
	    } catch(e) {
	        if(window.parent) {
	            try {
	                referrer = window.parent.document.referrer;
	            } catch(e2) {
	                referrer = '';
	            }
	        }
	    }

	    if(referrer==""|| referrer.indexOf("mengma")=="-1"){
	    	if(c_url){
    			window.location.href = c_url;
	    	}
	    }else{
	    	if(referrer.indexOf("404")>="1"){
	    		referrer = referrer.replace("\/404","");
	    		location.href = referrer;
	    	}else{
	    		if(c_url){
	    			if(window.location.href.indexOf('#') != -1) {
	    				c_url = window.location.href.split('=')[1];
	    				window.location.href = c_url;
	    			} else {
	    				location.href = c_url;
	    			}
		    	}
	    	}
	    }
	}
});
